<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px" :label-position="labelPosition">
      <!-- <el-form-item label="测评量表id" prop="scaleId" label-width="90px">
        <el-input
          v-model="queryParams.scaleId"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="测评量表:" prop="scaleTitle" label-width="5rem">
        <!-- <el-input
          v-model="queryParams.scaleTitle"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
        <el-select v-model="queryParams.scaleTitle" 
          @change="handleScaleId"
          clearable>
            <el-option v-for="(item) in scaleTitleList" 
            :label="item.scaleTitle" 
            :value="item.scaleTitle"
            :key="item.scaleTitle"
            @keyup.enter.native="handleQuery">
            </el-option>
          </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:result:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:result:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:result:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:result:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="resultList" @selection-change="handleSelectionChange" tooltip-effect="dark myTooltips">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="测评结果id" align="center" prop="resultId" />
      <el-table-column label="测评量表id" align="center" prop="scaleId" /> -->
     
      <el-table-column label="测评建议" align="center" prop="resultContent" :show-overflow-tooltip="true"/>
       
      <el-table-column label="分数范围和结果显示" align="center" prop="resultDetails" :show-overflow-tooltip="true"/>
      
      <el-table-column label="所属测评量表" align="center" prop="scaleTitle" />
        
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:result:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:result:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改测评结果参考信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="测评量表id" prop="scaleId">
          <el-input v-model="form.scaleId" placeholder="请输入测评量表id" />
        </el-form-item> -->
        <el-form-item label="测评量表标题:" prop="scaleTitle" label-width="7rem">
          <!-- <el-input v-model="form.scaleTitle" placeholder="请输入测评量表标题" /> -->
          <el-select v-model="form.scaleTitle" 
          @change="handleScaleId"
          clearable>
            <el-option v-for="(item) in scaleTitleList" 
            :label="item.scaleTitle" 
            :value="item.scaleTitle"
            :key="item.scaleTitle">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="结果详情:" prop="resultDetails" label-width="7rem" :autosize="{ minRows: 5, maxRows: 8}">
          <el-input v-model="form.resultDetails" type="textarea"  />
        </el-form-item>
        <el-form-item label="结论:" prop="resultContent" label-width="7rem" :autosize="{ minRows: 5, maxRows: 8}">
          <el-input v-model="form.resultContent" type="textarea" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listResult, getResult, delResult, addResult, updateResult } from "@/api/mentalTest/result.js";
import { listScale } from "@/api/mentalTest/scaleManage";

export default {
  name: "Result",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 测评结果参考信息表格数据
      resultList: [],
      //所属测评量表标题数据
      scaleTitleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        scaleId: null,
        scaleTitle: null,
        resultDetails:null,
        resultContent:null,
        resultId:null,
      },
      // 表单参数
      form: {
        scaleId: null,
        scaleTitle: null,
        resultDetails:null,
        resultContent:null,
        resultId:null,
      },
      // 表单校验
      rules: {
      },
      labelPosition: 'left',
    };
  },
  created() {
    this.getList();
    this.getTitle();
  },
  methods: {
    /** 查询测评结果参考信息列表 */
    getList() {
      this.loading = true;
      console.log("要查询的呀",this.queryParams);
      listResult(this.queryParams).then(response => {
        this.resultList = response.rows;
        console.log("总的list",response.rows);
        this.total = response.total;
        this.loading = false;
      });
    },
    //从questionInfo得到所有所属测评量表的标题
    getTitle() {
      // this.queryParams.scaleTitle = '所有';
      listScale(this.queryTitle).then(response =>{
        this.scaleTitleList = response.rows;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        resultId: null,
        scaleId: null,
        scaleTitle: null,
        resultDetails: null,
        resultContent: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.queryParams.scaleTitle = null;
      console.log("查询的",this.queryParams);
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.resultId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      // this.getTitle();
      this.open = true;
      this.title = "添加测评结果参考信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const resultId = row.resultId || this.ids
      getResult(resultId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改测评结果参考信息";
      });
    },
    //为得到scaleId的回调函数
    handleScaleId(selectedValue) {
      const selectedScale = this.scaleTitleList.find(item => item.scaleTitle === selectedValue);
      if (selectedScale) {
        const scaleId = selectedScale.scaleId;
        this.form.scaleId=scaleId
        this.queryParams.scaleId = scaleId
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.resultId != null) {
            updateResult(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addResult(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.reset();
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const resultIds = row.resultId || this.ids;
      this.$modal.confirm('是否确认删除测评结果参考信息编号为"' + resultIds + '"的数据项？').then(function() {
        return delResult(resultIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/result/export', {
        ...this.queryParams
      }, `result_${new Date().getTime()}.xlsx`)
    },
     
    
  }
};
</script>
<style>
.myTooltips{
  background-color: rgb(108, 195, 230)!important;
   color: #fff;
   border: 1px solid #333;
   max-width: 30%;
   text-indent:2em;
}
</style>
